package com.atguigu.juc;

/**
 * @author ：MJS
 * @date ：Created in 2021/4/26  14:23
 * @description：TODO
 */
public class DeadLockDemo {


    public static void main(String[] args) {
        final Object objectLockA = new Object();
        final Object objectLockB = new Object();
        new Thread(() -> {

            synchronized (objectLockA) {
                System.out.println(Thread.currentThread().getName() + "\t" + "自己拥有A，想获得B");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectLockB) {
                    System.out.println(Thread.currentThread().getName() + "\t" + " 成功获得B");
                }
            }
        }, "A").start();

        new Thread(() -> {

            synchronized (objectLockB) {
                System.out.println(Thread.currentThread().getName() + "\t" + "自己拥有B，想获得A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectLockA) {
                    System.out.println(Thread.currentThread().getName() + "\t" + " 成功获得A");
                }
            }
        }, "B").start();

    }
}


